﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using OurFAS.Data;
using System.IO;
using NPOI.HSSF.UserModel;

namespace OurFAS.MyDataManage
{
    public partial class FrmDataInput : Form
    {
        IList<Tbl_BaseAssets> m_assetsImport;

        public FrmDataInput()
        {
            InitializeComponent();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog oDlg = new OpenFileDialog();
            oDlg.Filter = "Excel Files (.xls)|*.xls";
            if (oDlg.ShowDialog() == DialogResult.OK)
            {
                tbFilePath.Text = oDlg.FileName;
            }
            //首先读取文件中的信息
            try
            {
                m_assetsImport = new List<Tbl_BaseAssets>();
                FileStream fileTemplate = new FileStream(tbFilePath.Text, FileMode.Open, FileAccess.Read);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(fileTemplate);
                //获取工作簿
                {
                    //读取设备信息
                    HSSFSheet sheetEqiupment = hssfworkbook.GetSheet("资产信息表") as HSSFSheet;

                    int rowCount = sheetEqiupment.LastRowNum;
                    int tempID = 0;
                    for (int i = 1; i <= rowCount; i++)
                    {
                        HSSFRow row = sheetEqiupment.GetRow(i) as HSSFRow;

                        Tbl_BaseAssets asset = new Tbl_BaseAssets();
                        if (row.GetCell(0) == null || row.GetCell(0).ToString() == "")
                        {
                            MessageBox.Show(string.Format("资产编号不能为空，请检查第{0}行数据", i));
                            break;
                        }
                        else
                        {
                            asset.AssetCode = row.GetCell(0).ToString();
                        }

                        if (row.GetCell(1) != null)
                        {
                            asset.AssetTagCode = row.GetCell(1).ToString();
                        }
                        else
                        {
                            asset.AssetTagCode = "";
                        }

                        if (row.GetCell(2) != null)
                        {
                            asset.AssetName = row.GetCell(2).ToString();
                        }
                        else
                        {
                            asset.AssetName = "";
                        }

                        if (row.GetCell(3) != null)
                        {
                            asset.AssetSpec = row.GetCell(3).ToString();
                        }
                        else
                        {
                            asset.AssetSpec = "";
                        }

                        if (row.GetCell(4) != null)
                        {
                            asset.TypeCodeL = row.GetCell(4).ToString();
                        }
                        else
                        {
                            asset.TypeCodeL = "";
                        }

                        if (row.GetCell(5) != null)
                        {
                            asset.TypeCodeM = row.GetCell(5).ToString();
                        }
                        else
                        {
                            asset.TypeCodeM = "";
                        }

                        if (row.GetCell(6) != null)
                        {
                            asset.UseFor = row.GetCell(6).ToString();
                        }
                        else
                        {
                            asset.UseFor = "";
                        }

                        if (row.GetCell(7) != null)
                        {
                            asset.InBillCode = row.GetCell(7).ToString();
                        }
                        else
                        {
                            asset.InBillCode = "";
                        }

                        if (row.GetCell(8) != null)
                        {
                            asset.ProductionFirm = row.GetCell(8).ToString();
                        }
                        else
                        {
                            asset.ProductionFirm = "";
                        }

                        if (row.GetCell(9) != null)
                        {
                            asset.OriCost = Convert.ToDecimal(row.GetCell(9).ToString());
                        }
                        else
                        {
                            asset.OriCost = 0;
                        }

                        if (row.GetCell(10) != null)
                        {
                            asset.StartUseDate = row.GetCell(10).ToString();
                        }
                        else
                        {
                            asset.StartUseDate = "19000101";
                        }

                        if (row.GetCell(11) != null)
                        {
                            asset.InCharge = row.GetCell(11).ToString();
                        }
                        else
                        {
                            asset.InCharge = "";
                        }

                        if (row.GetCell(12) != null)
                        {
                            asset.Dept = row.GetCell(12).ToString();
                        }
                        else
                        {
                            asset.Dept = "";
                        }

                        if (row.GetCell(13) != null)
                        {
                            asset.TaskCode = row.GetCell(13).ToString();
                        }
                        else
                        {
                            asset.TaskCode = "";
                        }

                        if (row.GetCell(14) != null)
                        {
                            asset.GetType = row.GetCell(14).ToString();
                        }
                        else
                        {
                            asset.GetType = "";
                        }

                        if (row.GetCell(15) != null)
                        {
                            asset.PurchaseFrom = row.GetCell(15).ToString();
                        }
                        else
                        {
                            asset.PurchaseFrom = "";
                        }

                        if (row.GetCell(16) != null)
                        {
                            asset.GBCode = row.GetCell(16).ToString();
                        }
                        else
                        {
                            asset.GBCode = "";
                        }

                        if (row.GetCell(17) != null)
                        {
                            asset.MeasureInfo = row.GetCell(17).ToString();
                        }
                        else
                        {
                            asset.MeasureInfo = "";
                        }

                        if (row.GetCell(18) != null)
                        {
                            asset.Location = row.GetCell(18).ToString();
                        }
                        else
                        {
                            asset.Location = "";
                        }

                        if (row.GetCell(19) != null)
                        {
                            asset.CertificateNo = row.GetCell(19).ToString();
                        }
                        else
                        {
                            asset.CertificateNo = "";
                        }

                        if (row.GetCell(20) != null)
                        {
                            asset.RepairDate =row.GetCell(20).ToString();
                        }
                        else
                        {
                            asset.RepairDate = "19000101";
                        }

                        m_assetsImport.Add(asset);
                        tempID++;
                    }
                }
            }
            catch
            {
                MessageBox.Show("Excel文件读取错误，请检查:\r\n1.您是否在其他程序中打开了被导入的Excel文件。\r\n2.Excel文件格式是否正确。");
                return;
            }
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
        }

        private void btnDataInput_Click(object sender, EventArgs e)
        {
            if (tbFilePath.Text == "")
            {
                MessageBox.Show("请打开要导入的数据文件！");
                return;
            }

            if (!File.Exists(tbFilePath.Text))
            {
                MessageBox.Show("数据文件不存在！");
                return;
            }
            if (MessageBox.Show(string.Format("数据导入将改变库存设备的记录，确认导入数据吗？"), "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                    //首先清空主资产表
                OurFasDBEntities entity = new OurFasDBEntities();
                foreach (Tbl_BaseAssets asset in entity.Tbl_BaseAssets.ToList())
                {
                    entity.Tbl_BaseAssets.DeleteObject(asset);
                }

                //其次依次插入新资产表
                foreach (Tbl_BaseAssets record in m_assetsImport)
                {
                    entity.Tbl_BaseAssets.AddObject(record);
                }
                try
                {
                    entity.SaveChanges();
                    MessageBox.Show("已完成对主资产表的数据清理操作！");
                }
                catch
                {
                    MessageBox.Show("在对主资产表进行数据清理操作时出错，请重试或联系管理员！");
                }
            }
            MessageBox.Show("数据导入成功！");
            this.Close();
        }
    }
}
